/*****************************************************************
ELDATool
Copyright (C) 2012 G. Fortino

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2.1 of the License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*****************************************************************/

//Generated by StarUML(tm) Java Add-In

//@ Project : Untitled
//@ File Name : Msg.java
//@ Date : 24/12/2007
//@ Author : 

package eldasim;

import jaf.Actor;

import java.io.Serializable;

import eldaframework.eldaevent.ELDAEvent;
import eldasim.handler.EventHandler;

public class Msg extends jaf.Message implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final byte ELDAFRAMEWORK_EVENT = 0, DELAYED_ELDAFRAMEWORK_EVENT = 01, READY_TO = 10, REMOTE_REQUEST = 20,
			REMOTE_NOTIFICATION = 21;

	public byte type;
	private Actor source;
	private Object cargo;
	private String sourceurl;
	private String targeturl;

	// Messaggi remoti
	public Msg(String sourceurl, String targeturl, byte type, Object cargo) {
		this.sourceurl = sourceurl;
		this.targeturl = targeturl;
		this.type = type;
		this.cargo = cargo;
	}

	// Messaggi locali
	public Msg(Actor receiver, byte type, Object cargo) {
		this.type = type;
		this.receiver = receiver;
		this.cargo = cargo;
	}

	public String getSourceURL() {
		return sourceurl;
	}

	public String getTargetURL() {
		return targeturl;
	}

	public Actor getSource() {
		return source;
	}

	public int getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public Object getCargo() {
		return cargo;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		int point = cargo.getClass().getName().lastIndexOf(".") + 1;
		int point2 = receiver.getClass().getName().lastIndexOf(".") + 1;
		if (sourceurl != null || targeturl != null)
			buffer.append("Msg( type: " + type + " sourceURL: " + sourceurl + " targetURL: " + targeturl + " cargo: "
					+ cargo.getClass().getName().substring(point) + " sender: " + ((ELDAEvent) cargo).getSource());
		else
			buffer.append("Msg( type: " + type + " as: " + ((EventHandler) receiver).getAS().getName() + " receiver: "
					+ receiver.getClass().getName().substring(point2) + " cargo: " + cargo.getClass().getName().substring(point) + " sender: "
					+ ((ELDAEvent) cargo).getSource());

		return buffer.toString();
	}

}
